/*
 * =====================================================================================
 *
 *       Filename:  cl_resource_guard.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  03/26/2011 09:43:49 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ken Chen (Ken), chen_kenz@emc.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#ifndef _CL_RESOURCE_GUARD_H_
#define _CL_RESOURCE_GUARD_H_

#include <cstddef>

namespace neo
{
template<typename T>
class CLNewDeleter
{
public:
  void operator()(const T* t) const
  {
    delete t;
  }
};

template<typename T>
class CLNewArrayDeleter
{
public:
  void operator()(const T* t) const
  {
    delete[] t;
  }
};

template<typename T, typename ResourceDeleter>
class CLResourceGuard
{
public:
  CLResourceGuard(T* t, ResourceDeleter rd)
    :m_t(t),
    m_rd(rd)
  {
  }

  ~CLResourceGuard()
  {
    m_rd(m_t);
  }

  void reset(T* t)
  {
    m_rd(m_t);
    m_t = t;
  }

  T* release(void)
  {
    T* t = m_t;
    m_t = NULL;
    return t;
  }

private:
  T* m_t;
  ResourceDeleter m_rd;
};

} // neo

#endif //_CL_RESOURCE_GUARD_H_

